﻿using System;
using System.Collections.Generic;
using System.Data;
using WebService.Nafania.DataLayer.Model.Base;

namespace WebService.Nafania.DataLayer.Model 
{	
	public partial class Template
	{
		public long Id { get; set; }
		public string Name { get; set; }
		public string Type { get; set; }
		public string Alias { get; set; }
		public int Version { get; set; }
		public string Value { get; set; }
		public string Comment { get; set; }
		public DateTime Date { get; set; }
		public string Key { get; set; }
 	}

	internal partial class TemplateRepository
	{
		public static TemplateRepository SyncAccess
		{
			get { return new TemplateRepository {Repository = new SyncAdoRepository()}; }
		}

		public static TemplateRepository AsyncAccess
		{
			get { return new TemplateRepository {Repository = new AsyncAdoRepository()}; }
		}

		protected TemplateRepository(){}

		protected AdoRepository Repository { get; set; }

		public static Template Mapping(IDataReader reader)
		{
			Template result = null;
			while (reader.Read())
			{
				result = ReadToEntity(reader);
			}
			return result;
		}

		public static List<Template> ListMapping(IDataReader reader)
		{
			List<Template> result = new List<Template>();
			while (reader.Read())
			{
				result.Add(ReadToEntity(reader));
			}
			return result;
		}

		private static Template ReadToEntity(IDataReader reader) 
		{
			return new Template
			       	{
								Id = reader.GetInt64("Id"),
								Name = reader.GetString("Name"),
								Type = reader.GetString("Type"),
								Alias = reader.GetString("Alias"),
								Version = reader.GetInt32("Version"),
								Value = reader.GetString("Value"),
								Comment = reader.GetStringOrNull("Comment"),
								Date = reader.GetDateTime("Date"),
								Key = reader.GetString("Key"),
			       	};
		}
 
  
		public Template GetById(long @id)
		{
			using (AdoRepository rep = Repository)
			{
				return rep.ExecuteProcedureReader("[shell].[Template_GetById]", null, Mapping);
			}

		}

		protected List<Template> GetAll(List<SpParam> sqlParams = null)
		{
			using (AdoRepository rep = Repository)
			{
				return rep.ExecuteProcedureReader("[shell].[Template_GetAll]", sqlParams, ListMapping);
			}
		}

		protected int GetCount(List<SpParam> sqlParams = null)
		{
			using (AdoRepository rep = Repository)
			{
				return Convert.ToInt32(rep.ExecuteProcedureScalar<Template>("[shell].[Template_Count]", sqlParams));
			}
		}

		public void Delete(long @id)
		{
			using (AdoRepository rep = Repository)
			{
				List<SpParam> sqlParams = new List<SpParam>
																	{
																		new SpParam {Name = "@Id", Type = DbType.Int64, Value = @id},
																	};
				rep.ExecuteProcedureNonQuery<Template>("[shell].[Template_Delete]", sqlParams);
			}
		}

		public Template Insert(Template @template)
		{
			using (AdoRepository rep = Repository)
			{
				List<SpParam> sqlParams = new List<SpParam>
																	{
																		new SpParam {Name = "@Name", Type = DbType.String, Value = @template.Name},
																		new SpParam {Name = "@Type", Type = DbType.String, Value = @template.Type},
																		new SpParam {Name = "@Alias", Type = DbType.String, Value = @template.Alias},
																		new SpParam {Name = "@Version", Type = DbType.Int32, Value = @template.Version},
																		new SpParam {Name = "@Value", Type = DbType.String, Value = @template.Value},
																		new SpParam {Name = "@Comment", Type = DbType.String, Value = @template.Comment},
																		new SpParam {Name = "@Date", Type = DbType.DateTime, Value = @template.Date},
																		new SpParam {Name = "@Key", Type = DbType.String, Value = @template.Key},
																	};
				return rep.ExecuteProcedureReader("[shell].[Template_Insert]", sqlParams, Mapping);
			}
		}


		public Template Update(Template @template)
		{
			using (AdoRepository rep = Repository)
			{
				List<SpParam> sqlParams = new List<SpParam>
																	{
																		new SpParam {Name = "@Name", Type = DbType.String, Value = @template.Name},
																		new SpParam {Name = "@Type", Type = DbType.String, Value = @template.Type},
																		new SpParam {Name = "@Alias", Type = DbType.String, Value = @template.Alias},
																		new SpParam {Name = "@Version", Type = DbType.Int32, Value = @template.Version},
																		new SpParam {Name = "@Value", Type = DbType.String, Value = @template.Value},
																		new SpParam {Name = "@Comment", Type = DbType.String, Value = @template.Comment},
																		new SpParam {Name = "@Date", Type = DbType.DateTime, Value = @template.Date},
																		new SpParam {Name = "@Key", Type = DbType.String, Value = @template.Key},
																	};
				return rep.ExecuteProcedureReader("[shell].[Template_Update]", sqlParams, Mapping);
			}
		}
	}
}